package com.example.demo.controller;

import com.example.demo.common.convention.result.Result;
import com.example.demo.common.convention.result.Results;
import com.example.demo.dto.request.CaptchaReq;
import com.example.demo.dto.request.CaptchaResultReq;
import com.example.demo.service.CaptchaService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;

@RestController
@RequestMapping("/api/captcha")
@RequiredArgsConstructor
public class CaptchaController {
    final private CaptchaService captchaService;


    @GetMapping("/getCaptcha")
    public DeferredResult<Object> getCaptcha(HttpServletRequest request) {
        String userId = request.getAttribute("userId").toString();
        return captchaService.getCaptcha(userId);
    }

    @PostMapping("/postCaptcha")
    public Result<Void> postCaptcha(@RequestBody CaptchaReq req){
        captchaService.postCaptcha(req.getUserid(),req.getCode());
        return Results.success();
    }

    @GetMapping("/getCaptchaResult")
    public DeferredResult<Object> getCaptchaResult(HttpServletRequest request) {
        String userId = request.getAttribute("userId").toString();
        return captchaService.getCaptchaResult(userId);
    }

    @PostMapping("/postCaptchaResult")
    public Result<Void> postCaptchaResult(@RequestBody CaptchaResultReq req){
        captchaService.postCaptchaResult(req.getUserid(),req.getStatus());
        return Results.success();
    }
}
